cap log close
snapshot erase _all
clear all
estimates drop _all

*===========================*
*	Set Globals				*
*===========================*

	global filenum 10
	global filename "main_DDD"

*===========================*
*	Set Locals				*
*===========================*
	
	#delimit ;
		local m_list 		"12 1 3 4 5 6";
		
		local keep_list1 	"m12_x_small_y2020 m1_x_small_y2020 
							 m3_x_small_y2020 m4to6_x_small_y2020";
							 
		local keep_list2 	"m12_x_small_y2020 m1_x_small_y2020 
							 m3_x_small_y2020 m4_x_small_y2020 m5_x_small_y2020 
							 m6_x_small_y2020";							 

		local keep_list3 	"m12_x_small_y2020 m1_x_small_y2020 
							 m3_x_small_y2020 m4_x_small_y2020 m5_x_small_y2020 
							 m6_x_small_y2020 m4to6_x_small_y2020";	
						 
		local fixed_effect	"datevar#statefip";		// user supplied fixed effects;
		
		local outcomes 		"absent_paid";			// user supplied outcomes;
	
	#delimit cr
	
*===========================*
*	Load In Analysis File	*
*===========================*
	log using "${log_path}/${filenum}_${filename}.smcl", replace
	
	use "${data_path}/analysis_file.dta" , clear
	
	snapshot save
	
*===================*
* 	DD in Time		*
* 	18/19 vs 20		*
*===================*

local counter = 0
foreach v in `outcomes'{
	
	forvalues t=0/1{
		
		local counter = `counter' + 1
		if `counter' == 1			local r "replace"
		if `counter' > 	1			local r "append"
		snapshot restore 1
		
		keep if tw_binary_10ly==`t'
		
		#delimit ;
		
			** SIMPLE DD **;
				reghdfe `v' 
						m12-m3 m4to6 y2020 small
						small_x_y2020
						m12_x_y2020-m3_x_y2020 m4to6_x_y2020
						m12_x_small-m3_x_small m4to6_x_small
						m12_x_small_y2020 - m3_x_small_y2020 m4to6_x_small_y2020
						[w=wtfinl], a(`fixed_effect') vce(cluster cpsidp);
									
				test m12_x_small_y2020 = m1_x_small_y2020 = m3_x_small_y2020 = 0;
					scalar pval=`r(p)';					

				qui sum `v' if month==2 & asec_year!=2020 & small==1;
					scalar control_mean = `r(mean)';

				est sto ddd1_tw`t';
				estadd local 	controls 	"NO";
				estadd local 	spec		"DDD";
				estadd local 	samp 		"TW `t'";
				estadd scalar 	cmean 		control_mean;
				estadd scalar 	parallel 	pval;	
				
				regsave `keep_list1'
					using "${out_path}/${filenum}_`v'.dta",
					addlabel(outcome, "`v'", spec, "Simple DD", controls, "NO", sample, "TW`t'", mu, `=control_mean') `r';
					
			** DYNAMIC DD **;
				reghdfe `v' 
						m12-m6 y2020 small
						small_x_y2020
						m12_x_y2020-m6_x_y2020
						m12_x_small-m6_x_small
						m12_x_small_y2020 - m6_x_small_y2020
						[w=wtfinl], a(`fixed_effect') vce(cluster cpsidp);
									
				test m12_x_small_y2020 = m1_x_small_y2020 = m3_x_small_y2020 = 0;
					scalar pval=`r(p)';					

				qui sum `v' if asec_year!=2020 & small==1 [w=wtfinl];
					scalar control_mean = `r(mean)';

				est sto ddd2_tw`t';
				estadd local 	controls 	"NO";
				estadd local 	spec		"DDD";
				estadd local 	samp 		"TW `t'";
				estadd scalar 	cmean 		control_mean;
				estadd scalar 	parallel 	pval;	
				
				regsave `keep_list2'
					using "${out_path}/${filenum}_`v'.dta",
					addlabel(outcome, "`v'", spec, "Dynamic DD", controls, "NO",  sample, "TW`t'", mu, `=control_mean') append;
					
			** SIMPLE DD WITH CONTROLS **;
				
				reghdfe `v' 
						m12-m3 m4to6 y2020 small
						small_x_y2020
						m12_x_y2020-m3_x_y2020 m4to6_x_y2020
						m12_x_small-m3_x_small m4to6_x_small
						m12_x_small_y2020 - m3_x_small_y2020 m4to6_x_small_y2020
						bachelors age age2 childUnder12
						[w=wtfinl], a(`fixed_effect') vce(cluster cpsidp);
								
					test m12_x_small_y2020 = m1_x_small_y2020 = m3_x_small_y2020 = 0;
						scalar pval=`r(p)';					
									
					est sto ddd3_tw`t';
					estadd local  	controls 	"YES";
					estadd local 	spec		"DDD";
					estadd local 	samp 		"TW `t'";
					estadd scalar 	cmean 		control_mean;
					estadd scalar 	parallel 	pval;	
					
				regsave `keep_list1'
					using "${out_path}/${filenum}_`v'.dta",
					addlabel(outcome, "`v'", spec, "Simple DD", controls, "YES", sample, "TW`t'", mu, `=control_mean') append ;					
			
			** DYNAMIC DD WITH CONTROLS **;
			
				reghdfe `v' 
						m12-m6 y2020 small
						small_x_y2020
						m12_x_y2020-m6_x_y2020
						m12_x_small-m6_x_small
						m12_x_small_y2020 - m6_x_small_y2020
						bachelors age age2 childUnder12
						[w=wtfinl], a(`fixed_effect') vce(cluster cpsidp);
							
				test m12_x_small_y2020 = m1_x_small_y2020 = m3_x_small_y2020 = 0;
					scalar pval=`r(p)';					
								
				est sto ddd4_tw`t';
				estadd local  	controls 	"YES";
				estadd local 	spec		"DDD";
				estadd local 	samp 		"TW `t'";
				estadd scalar 	cmean 		control_mean;
				estadd scalar 	parallel 	pval;	
				
			regsave `keep_list2'
				using "${out_path}/${filenum}_`v'.dta",
				addlabel(outcome, "`v'", spec, "Dynamic DD", controls, "YES", sample, "TW`t'", mu, `=control_mean') append ;
			
		#delimit cr
		
	} // end telework loop
	
	
	** MAKE TABLE **
	#delimit ;
		esttab 	*tw0 *tw1
			using "${out_path}/${filenum}_`v'.csv",
			replace	label se star(* 0.1 ** .05 *** 0.01)
			s(cmean spec samp controls parallel N, 
				label("Control Mean" "Model" "Sample" "Controls" "Parallel Trends" "Observations"))
			keep(`keep_list3');	
	
	#delimit cr
	
} // end outcomes loop
log close
